package com.my.xinyidaixi.Activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.flipboard.bottomsheet.BottomSheetLayout;
import com.my.xinyidaixi.Adapter.GoodsAdapter;
import com.my.xinyidaixi.Adapter.TabFragmetPagerAdapter;
import com.my.xinyidaixi.Adapter.bottomselectgoodAdapter;
import com.my.xinyidaixi.Adapter.gaoduanxihuGoodAdapter;
import com.my.xinyidaixi.Adapter.jiafangGoodAdapter;
import com.my.xinyidaixi.Adapter.kuziqunziGoodAdapter;
import com.my.xinyidaixi.Adapter.peijianGoodAdapter;
import com.my.xinyidaixi.Adapter.pijuhuliGoodAdapter;
import com.my.xinyidaixi.Adapter.xixieGoodAdapter;
import com.my.xinyidaixi.DataBase.Model.Over_UserModer;
import com.my.xinyidaixi.DataBase.Model.UserModel;
import com.my.xinyidaixi.Fragment.gaoduanxihufragment;
import com.my.xinyidaixi.Fragment.jiafang_Fragment;
import com.my.xinyidaixi.Fragment.jianxifragment;
import com.my.xinyidaixi.Fragment.kuziqunzi_Fragment;
import com.my.xinyidaixi.Fragment.peijian_Fragment;
import com.my.xinyidaixi.Fragment.pijuhuli_Fragment;
import com.my.xinyidaixi.Fragment.xixiefragment;
import com.my.xinyidaixi.Interface.Goods_interface;
import com.my.xinyidaixi.Model.GoodsItem;
import com.my.xinyidaixi.Overwrite.NoScrollViewPager;
import com.my.xinyidaixi.R;
import com.my.xinyidaixi.Util.ToastUtil;

import java.util.ArrayList;

/**
 * Created by zw on 2017/6/6.
 */
public class jianxi extends AppCompatActivity implements Goods_interface{

    private TabFragmetPagerAdapter fAdapter;

    private ImageView imageview_fanhui;

    private Intent intent;

    private TextView tv_count,tv_submit;

    private BottomSheetLayout bottomsheetlayout;

    private LinearLayout bottomsheetbutton;

    private View bottomsheet;

    private bottomselectgoodAdapter bottomselectadapter;

    private RecyclerView reSelected;

    private SparseArray<GoodsItem> selectedList;

   // private DecimalFormat df;

    private GoodsAdapter goodsAdapter;

    private xixieGoodAdapter xixieAdapter;

    private gaoduanxihuGoodAdapter gaoduanxihuAdapter;

    private kuziqunziGoodAdapter kuziqunziAdpter;

    private peijianGoodAdapter peijianAdapter;

    private jiafangGoodAdapter jiafangAdapter;

    private pijuhuliGoodAdapter pijuhuliGoodAdapter;

    private int cost;

    private UserModel moder=new Over_UserModer();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zhuanyeqingxi_fragment);
       // df = new DecimalFormat("#.00");

        selectedList=new SparseArray<>();
        initdata();
    }


    public void initdata()
    {
        imageview_fanhui = (ImageView) findViewById(R.id.imageview_fanhui);

        imageview_fanhui.setOnClickListener(new jianxibutton());

        tv_count = (TextView) findViewById(R.id.tv_Count);

        bottomsheetlayout = (BottomSheetLayout) findViewById(R.id.bottomSheetLayout);

        bottomsheetbutton = (LinearLayout) findViewById(R.id.bottomSheetButton);

        bottomsheetbutton.setOnClickListener(new jianxibutton());

        tv_submit = (TextView) findViewById(R.id.tv_submit);//获取父activity的控件

        tv_submit.setOnClickListener(new jianxibutton());


        FragmentManager fm=getSupportFragmentManager();

        //实现 fragment 与Viewpage的联动
        Fragment tabFrames[] = new Fragment[]{new jianxifragment(), new xixiefragment(),new gaoduanxihufragment(),new kuziqunzi_Fragment(),new peijian_Fragment(),new jiafang_Fragment(),new pijuhuli_Fragment()};

        String tabTitles[] = new String[]{"上衣","大衣外套", "鞋类","裤子裙子","配件","家纺类","皮具护理"};

        final TabLayout tablayout = (TabLayout) findViewById(R.id.tablayout);

        tablayout.setTabMode(TabLayout.MODE_SCROLLABLE);

        NoScrollViewPager viewpage = (NoScrollViewPager) findViewById(R.id.viewpageconter);
        //实现Tabfragmetpageradapter适配器

        fAdapter = new TabFragmetPagerAdapter(fm, tabTitles, tabFrames);
        //根据tabtitles的长度 添加Tab

        for (int i = 0; i < tabTitles.length; i++) {
            tablayout.addTab(tablayout.newTab().setText(tabTitles[i]));
        }
        viewpage.setPagingEnabled(false);//禁止viewpage滑动

        viewpage.setAdapter(fAdapter); //使fragment跟着viewpage动

        viewpage.setOffscreenPageLimit(3); //表示三个界面之间来回切换都不会重新加载

        intent=getIntent();

        viewpage.setCurrentItem(intent.getIntExtra("position",-1)); //如果没有取到值 返回为-1

        tablayout.setupWithViewPager(viewpage);//viewpage跟着Tablayout动
        //实现 fragment 与Viewpage的联动
    }

    //弹出底部弹出购物车清单
    private void showBottomsheet()
    {
        if(bottomsheet==null)
        {
            bottomsheet=CreateBottomSheetView();
        }
        if(bottomsheetlayout.isSheetShowing())
        {
            bottomsheetlayout.dismissSheet();
        }
        else
        {
            if(selectedList.size()!=0)
            {
                bottomsheetlayout.showWithSheetView(bottomsheet);
            }
            else
            {
                Toast.makeText(jianxi.this,"selectedList大小为"+selectedList.size(),Toast.LENGTH_SHORT).show();
            }
        }
    }

    //创建底部弹出购物车清单
    private View CreateBottomSheetView()
    {
        View view= LayoutInflater.from(jianxi.this).inflate(R.layout.layout_bottom_sheet,null);

        reSelected = (RecyclerView) view.findViewById(R.id.selectRecyclerView);

        reSelected.setLayoutManager(new LinearLayoutManager(jianxi.this));

        TextView clear = (TextView) view.findViewById(R.id.clear);

        clear.setOnClickListener(new jianxibutton());

        bottomselectadapter = new bottomselectgoodAdapter(jianxi.this,selectedList);

        reSelected.setAdapter(bottomselectadapter);

        return  view;
    }

    //重写接口
    @Override
    public void add(GoodsItem item) {
        GoodsItem temp=selectedList.get(item.id);
        if(temp==null)
        {
            item.count=1;
            selectedList.append(item.id,item); //向sparelist添加数据
        }
        else
        {
            temp.count++;
        }
        update(true);
    }

    @Override
    public void remove(GoodsItem item)
    {
        GoodsItem temp=selectedList.get(item.id);
        if(temp!=null)
        {
            if(temp.count<2)
            {
                selectedList.remove(item.id);//向sparelist删除数据
            }
            else
            {
                item.count--;
            }
        }
        update(true);
    }

    @Override
    public int getSelectedItemCountById(int id)
    {
        GoodsItem temp=selectedList.get(id);
        if(temp==null)
        {
            return 0;
        }
        return temp.count;
    }

    @Override
    public void clear()
    {
        selectedList.clear();
        update(true);
    }

    @Override
    public void update(boolean refreshGoodList) {
        int size=selectedList.size();
        if(size>0)
        {
            tv_submit.setEnabled(true);
            tv_submit.setBackgroundColor(Color.parseColor("#FF00DBF5"));
        }
        else
        {
            tv_submit.setEnabled(false);
            tv_submit.setBackgroundColor(Color.parseColor("#b5b5b5"));
        }
        int cost=0;
        for(int i=0;i<size;i++)
        {
            GoodsItem item=selectedList.valueAt(i);
            cost+=item.count*item.price;
        }
        tv_count.setText(String.valueOf(cost));

        this.cost=cost;//定义到全局cost

        if(goodsAdapter!=null && refreshGoodList)
        {
            goodsAdapter.notifyDataSetChanged();
        }
        if(xixieAdapter!=null && refreshGoodList)
        {
            xixieAdapter.notifyDataSetChanged();
        }
        if(gaoduanxihuAdapter!=null&& refreshGoodList)
        {
            gaoduanxihuAdapter.notifyDataSetChanged();
        }

        if(kuziqunziAdpter!=null&& refreshGoodList)
        {
            kuziqunziAdpter.notifyDataSetChanged();
        }

        if(peijianAdapter!=null&& refreshGoodList)
        {
            peijianAdapter.notifyDataSetChanged();
        }

        if(jiafangAdapter!=null&& refreshGoodList)
        {
            jiafangAdapter.notifyDataSetChanged();
        }

        if(pijuhuliGoodAdapter!=null&& refreshGoodList)
        {
            pijuhuliGoodAdapter.notifyDataSetChanged();
        }

        if(bottomselectadapter!=null)
        {
            bottomselectadapter.notifyDataSetChanged();
        }
        if(bottomsheetlayout.isSheetShowing()&&selectedList.size()<1)
        {
            bottomsheetlayout.dismissSheet();
        }
    }
    @Override
    public void goods_adapter(GoodsAdapter goodsAdapter)
    {
        this.goodsAdapter=goodsAdapter;
    }

    @Override
    public void goods_xixieadapter(xixieGoodAdapter xixieAdapter)
    {
        this.xixieAdapter=xixieAdapter;
    }

    @Override
    public void goods_gaoduanxihuadapter(gaoduanxihuGoodAdapter gaoduanxihuAdapter)
    {
        this.gaoduanxihuAdapter=gaoduanxihuAdapter;
    }

    @Override
    public void goods_kuziqunziadapter(kuziqunziGoodAdapter kuziqunziAdapter) {
        this.kuziqunziAdpter=kuziqunziAdapter;
    }

    @Override
    public void goods_peijianadapter(peijianGoodAdapter peijianAdapter) {
        this.peijianAdapter=peijianAdapter;
    }

    @Override
    public void goods_jiafangadapter(jiafangGoodAdapter jiafangAdapter) {
        this.jiafangAdapter=jiafangAdapter;
    }

    @Override
    public void goods_pijuhuliadapter(pijuhuliGoodAdapter pijuhuliAdapter) {
        this.pijuhuliGoodAdapter=pijuhuliAdapter;
    }


    //重写接口

    //返回按钮
    public class jianxibutton implements View.OnClickListener
    {
        @Override
        public void onClick(View v)
        {
            switch (v.getId())
            {
                case R.id.imageview_fanhui:
                    jianxi.this.finish();
                    overridePendingTransition(0,R.anim.ani_left_sign_out);//窗口向左滑出
                    break;
                case R.id.tv_submit:
                   // Toast.makeText(jianxi.this,"selected大小"+selectedList.size(),Toast.LENGTH_SHORT).show();
                    if(moder.count()==0)
                    {
                        Intent intent=new Intent();
                        intent.setClass(jianxi.this,login_activity.class);
                        startActivity(intent);
                        jianxi.this.overridePendingTransition(R.anim.ani_right_get_into,R.anim.ani_left_sign_out);
                        ToastUtil.shortToast(jianxi.this,"请先登录");
                        return;
                    }
                    ArrayList<GoodsItem> arrayList=new ArrayList<GoodsItem>();
                    for(int i=0;i<selectedList.size();i++)
                    {
                        arrayList.add(selectedList.valueAt(i));
                    }
                    Intent intent=new Intent();
                    Bundle bundle=new Bundle();
                    bundle.putSerializable("arraylist",arrayList);
                    bundle.putInt("cost",cost);
                    intent.putExtras(bundle);
                    intent.setClass(jianxi.this,tijiao_Order.class);
                    startActivityForResult(intent,3);
                    jianxi.this.overridePendingTransition(R.anim.ani_right_get_into,R.anim.ani_left_sign_out);
                    break;
                case R.id.clear:
                    clear();
                    break;
                case R.id.bottomSheetButton:
                    showBottomsheet();
                    break;
                default:
                    break;
            }
        }
    }

    //按返回键 窗口向左滑出
    @Override
    protected void onPause()
    {
        super.onPause();
        jianxi.this.overridePendingTransition(R.anim.ani_right_get_into,R.anim.ani_left_sign_out);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==resultCode)
        {
            jianxi.this.finish();
            ToastUtil.shortToast(jianxi.this,"订单已生成，刷新一下吧！");
            jianxi.this.overridePendingTransition(R.anim.ani_right_get_into,R.anim.ani_left_sign_out);
        }
    }
}
